﻿package cn.smbms.service.user;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;



import cn.smbms.mapper.user.UserMapper;
import cn.smbms.pojo.User;

/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */
@Service("userService")
public class UserServiceImpl implements UserService{
	Logger log = Logger.getLogger(UserServiceImpl.class);
	@Resource
	private UserMapper userMapper;
	
	@Override
	public boolean add(User user) {
		int updateRows = userMapper.add(user);
		if(updateRows > 0){
			log.info("add success!");
			return true;
		}else{
			log.info("add failed!");
			return false;
		}
	}
	@Override
	public User login(String userCode, String userPassword) {
		User user = null;
		user = userMapper.getLoginUser(userCode);
		//匹配密码
		if(null != user){
			if(!user.getUserPassword().equals(userPassword))
				user = null;
		}
		
		return user;
	}
	@Override
	public List<User> getUserList(String queryUserName,int queryUserRole,int currentPageNo, int pageSize) {
		return userMapper.getUserList(queryUserName,queryUserRole,currentPageNo,pageSize);
	}
	@Override
	public User selectUserCodeExist(String userCode) {
		return userMapper.getLoginUser(userCode);
	}
	@Override
	public boolean deleteUserById(Integer delId) {
		return userMapper.deleteUserById(delId) > 0?true:false;
	}
	@Override
	public User getUserById(String id) {
		return userMapper.getUserById(id);
	}
	@Override
	public boolean modify(User user) {
		return userMapper.modify(user) > 0 ? true : false;
	}
	@Override
	public boolean updatePwd(int id, String pwd) {
		return userMapper.updatePwd(id,pwd) > 0 ? true : false;
	}
	@Override
	public int getUserCount(String queryUserName, int queryUserRole) {
		return userMapper.getUserCount(queryUserName,queryUserRole);
	}
	
}
